using System.Collections.Generic;
using System.Linq;
using CthulhuDomain.Strategies;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace CthulhuDomain.GameEntities
{
  public sealed class Player : DomainEntity
  {
    #region ctor
    public Player()
    {
      pos = new Vector2Adapter(0, 0);
      FrameColumns = 19;
      Shoots = new List<DomainEntity>();
    }

    public Player(IVector2 position)
    {
      pos = position;
      Shoots = new List<DomainEntity>();
    }

    #endregion

    #region props

    private int _height = 50;
    public override int Height
    {
      get { return _height; }
      set { _height = value; }
    }

    private int _width = 48;
    public override int Width
    {
      get { return _width; }
      set { _width = value; }
    }

    float speed = 4f;
    public override float Speed
    {
      get
      {
        return speed;
      }
      set
      {
        speed = value;
      }
    }

    public override IVector2 Position
    {
      get
      {
        return pos;
      }
      set
      {
        pos = value;
      }
    }
    #endregion

    public override int Damage
    {
      get { return 1; }
    }

    public override void Update(IRectangle context, Keys[] keys)
    {
      Move(context, keys);

      RemoveEntityOutOfContext();
      foreach (var item in Shoots.Where(item => item != null))
      {
        item.Speed = BulletSpeed;
        item.Move(context);
      }
    }

    #region draw
    public override void Draw(ISpriteBatch spriteBatch)
    {
      base.Draw(spriteBatch);

      RemoveEntityOutOfContext();

      foreach (var item in Shoots.Where(item => item != null))
        item.Draw(spriteBatch);
    }

    #endregion

    internal override void AddShoot()
    {
      var s = new Shoot(new Vector2Adapter(Position.X, Position.Y - Height)) { Texture = ProtoTypeChild.Texture, Width = ProtoTypeChild.Width, Height = ProtoTypeChild.Height};
      s.MovementStrategy = new VerticalMovement(s, eDirection.Up);
      Shoots.Add(s);
    }
  }
}